using System.Linq;
using System.Collections.Generic;

namespace PokerStar.Core.Rules
{
    public class FullhouseRule : BasicRule
    {
        public FullhouseRule(IRule next)
            : base(next)
        {
        }


        protected override GameResult Match(RuleContext context)
        {
            // make the groups --> 3, 2(big), 2(small)
            //var groups = cards.GroupBy(p => p.Number).
            //    OrderByDescending(p => p.Count()).
            //    ThenByDescending(p => CardNumberComparer.Card2Int(p.First().Number)).
            //    ToArray();

            //if (groups[0].Count() == 3 && groups[1].Count() == 2)
            //{
            //    var rs = new FullhouseGameResult();
            //    rs.Evidence = new List<int>
            //        {
            //            CardNumberComparer.Card2Int(groups[0].First().Number),
            //            CardNumberComparer.Card2Int(groups[1].First().Number),
            //            0,0,0
            //        };
            //    rs.Cards = groups[0].Union(groups[1]).ToList();
            //    return rs;
            //}

            if (context.GroupByNumber[0].Count() == 3 &&
                context.GroupByNumber[1].Count() == 2)
            {
                var group0 = context.GroupByNumber[0];
                var group1 = context.GroupByNumber[1];

                var rs = new FullhouseGameResult();
                rs.Evidence = new List<int>
                        {
                            CardNumberComparer.Card2Int(group0.First().Number),
                            CardNumberComparer.Card2Int(group1.First().Number),
                            0,0,0
                        };
                rs.Cards = group0.Union(group1).ToList();
                return rs;
            }

            return null;
        }
    }

    public class FullhouseGameResult : GameResult
    {
        public override ResultLevel Level
        {
            get { return ResultLevel.Fullhouse; }
        }

        public override string ToString()
        {
            return string.Format("Fullhouse -> {0} {1} {2} {3} {4}", Cards[0], Cards[1], Cards[2], Cards[3], Cards[4]);
        }
    }
}